<template>
  <el-dialog :title="title" width="80%" :visible.sync="visible" top="5vh" :close-on-click-modal="false">
    <el-form :inline="true">
      <el-form-item label="类别名称：">
        <el-input v-model="recordInfo.queryForm.categoryName" placeholder="输入类别名称" clearable />
      </el-form-item>
      <el-form-item label="上级类别：">
        <el-cascader
          v-model="recordInfo.searchParentIdArray"
          placeholder="请选择类别"
          :props="props"
          clearable
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="searchClick">搜索</el-button>
      </el-form-item>
      <el-form-item>
        <el-button v-if="true === showConfirmButton" type="primary" @click="determineClick">添加</el-button>
      </el-form-item>
    </el-form>
    <el-table
      ref="multipleTable"
      v-loading="recordInfo.loading"
      class="table-default"
      :data="recordInfo.list"
      border
      @selection-change="handleSelectionChange"
    >
      <el-table-column v-if="false===single" type="selection" width="43" align="center" fixed="left" />
      <el-table-column v-if="true===single" label="选择" width="60" fixed="right" align="center">
        <template v-slot="scope">
          <div class="list-operate-menu">
            <i class="el-icon-success check-button" @click="confirmSelect(scope.row)" />
          </div>
        </template>
      </el-table-column>
      <el-table-column label="一级类别" prop="level1GoodsCategoryName" align="center" />
      <el-table-column label="二级类别" prop="level2GoodsCategoryName" align="center" />
      <el-table-column label="三级类别" prop="categoryName" align="center" />
    </el-table>
    <el-pagination
      style="margin-top: 10px;"
      background
      :current-page.sync="recordInfo.queryForm.page"
      :page-size.sync="recordInfo.queryForm.pageSize"
      :page-sizes="[5,10,20,30,50,100,200,500]"
      layout="total, sizes, prev, pager, next, jumper"
      :total="recordInfo.total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </el-dialog>
</template>

<script>
import filters from '@/filters/index'
import api from '@/api'

export default {
  components: {},
  filters: {
    ...filters
  },
  mixins: [],
  data() {
    return {
      props: {
        lazy: true,
        lazyLoad(node, resolve) {
          setTimeout(() => {
            if (node.level === 0) {
              const arr = []
              api.getGoodsCategoryList({
                parentId: 0,
                page: 1,
                pageSize: 999
              }).then(response => {
                response.list.forEach((item, index) => {
                  arr.push({
                    value: item.goodsCategoryId,
                    label: item.categoryName,
                    leaf: false
                  })
                })
                resolve(arr)
              })
            }
            if (node.level === 1) {
              const arr = []
              api.getGoodsCategoryList({
                parentId: node.value,
                page: 1,
                pageSize: 999
              }).then(response => {
                response.list.forEach((item, index) => {
                  arr.push({
                    value: item.goodsCategoryId,
                    label: item.categoryName,
                    leaf: true
                  })
                })
                resolve(arr)
              })
            }
          }, 100)
        }
      },
      searchParentIdArray: [],
      recordInfo: {
        queryForm: {
          page: 1,
          pageSize: 10,
          level: 3,
          parentId: undefined,
          returnAllParentName: true,
          returnCount: true,
          orderBy: 'createTimeDesc'
        },
        list: [],
        total: 0
      },
      title: '选择商品类别',
      visible: false,
      handleSelection: [],
      showConfirmButton: true,
      single: false,
      callback: {}
    }
  },
  computed: {},
  methods: {
    handleSizeChange(e) {
      this.recordInfo.queryForm.pageSize = e
      this.getList()
    },
    handleCurrentChange(e) {
      this.recordInfo.queryForm.page = e
      this.getList()
    },
    getList() {
      this.recordInfo.queryForm.returnCount = this.recordInfo.queryForm.page === 1
      this.recordInfo.loading = true
      if (undefined !== this.recordInfo.searchParentIdArray && this.recordInfo.searchParentIdArray.length > 1) {
        this.recordInfo.queryForm.parentId = this.recordInfo.searchParentIdArray[1]
      } else {
        delete this.recordInfo.queryForm.parentId
      }
      this.$api.getGoodsCategoryList(this.recordInfo.queryForm, { showLoading: false }).then(res => {
        this.recordInfo.loading = false
        this.recordInfo.list = res.list
        if (this.recordInfo.queryForm.page === 1) {
          this.recordInfo.total = parseInt(res.count)
        }
      })
    },
    open(params, callback) {
      this.showConfirmButton = true
      this.single = undefined === params.single ? false : params.single
      this.callback = callback
      this.getList()
      this.visible = true
    },
    handleSelectionChange(e) {
      this.handleSelection = e
    },
    determineClick() {
      if (this.handleSelection.length === 0) {
        this.$message({
          message: '请勾选供应商',
          type: 'warning'
        })
        return
      }
      this.visible = false
      this.callback(this.handleSelection)
      this.$refs.multipleTable.clearSelection()
    },
    confirmSelect(item) {
      this.visible = false
      this.$parent.fatherMethodShop([item])
    },
    searchClick() {
      this.getList()
    }
  }
}
</script>

<style scoped>
.check-button {
  font-size: 20px !important;

  &:hover {
    color: #3a8ee6;
  }
}
</style>
